# package
require("readr")
require("dplyr")
require("tidyr")
require("stringr")
require("lme4")
require("modelsummary")

# set working directory
setwd()

# load
votematch <- read_csv(
  "2022VOTEMATCH20220812.csv", locale = locale(encoding = "SJIS")
)
votematch$DATE <- as.POSIXct(as.character(votematch$DATE), format = "%Y-%m-%d %H:%M:%S", tz ='Japan')
votematch <- votematch[votematch$DATE <= as.POSIXct("2022-07-10 20:00:00", tz="Japan"),]
votematch <- votematch[!is.na(votematch$DATE) & votematch$AGE != 1,]

# time
votematch$TIME <- NA
for (i in 1:18) {
  votematch$TIME[votematch$DATE >= as.POSIXct("2022-06-23 00:00:00", tz="Japan") + (i-1)*86400 & 
                 votematch$DATE < as.POSIXct("2022-06-23 00:00:00", tz="Japan") + i*86400] <- i
}
table(votematch$TIME)

# aggregate values by prefecture
votematch.prefecture <- votematch %>% 
  mutate(LDP = if_else(PARTY == 1, 1, 0), 
         Q18 = recode(Q18, `1` = 5, `2` = 4, `3` = 3, 
                      `4` = 2, `5` = 1, `99` = NA_real_),
         MALE = if_else(SEX == 1, 1, 0), 
         FEMALE = if_else(SEX == 2, 1, 0), 
         AGE20 = if_else(AGE %in% c(2, 3), 1, 0), 
         AGE30 = if_else(AGE == 4, 1, 0), 
         AGE40 = if_else(AGE == 5, 1, 0), 
         AGE50 = if_else(AGE == 6, 1, 0), 
         AGE60 = if_else(AGE == 7, 1, 0), 
         AGE70 = if_else(AGE %in% c(8, 9), 1, 0)) %>% 
  group_by(PREFECTURE, TIME) %>% 
  summarise(LDP = mean(LDP, na.rm = TRUE), 
            Q18 = mean(Q18, na.rm = TRUE), 
            SIZE = n(), 
            MALE = mean(MALE, na.rm = TRUE), 
            FEMALE = mean(FEMALE, na.rm = TRUE), 
            AGE20 = mean(AGE20, na.rm = TRUE), 
            AGE30 = mean(AGE30, na.rm = TRUE), 
            AGE40 = mean(AGE40, na.rm = TRUE), 
            AGE50 = mean(AGE50, na.rm = TRUE), 
            AGE60 = mean(AGE60, na.rm = TRUE), 
            AGE70 = mean(AGE70, na.rm = TRUE)) %>% 
  ungroup() %>% 
  mutate(ASSASSINATION = ifelse(TIME %in% c(16, 17, 18), 1, 0), 
         NARA = ifelse(PREFECTURE == 29, 1, 0), 
         YAMAGUCHI = ifelse(PREFECTURE == 35, 1, 0), 
         BLOCK = case_when(PREFECTURE == 1 ~ "Hokkaido",
                           PREFECTURE %in% 2:7 ~ "Tohoku", 
                           PREFECTURE %in% 8:11 ~ "Kita Kanto", 
                           PREFECTURE %in% c(12, 14, 19) ~ "Minami Kanto", 
                           PREFECTURE == 13 ~ "Tokyo", 
                           PREFECTURE %in% c(15:18, 20) ~ "Hokuriku Shin-etsu", 
                           PREFECTURE %in% 21:24 ~ "Tokai", 
                           PREFECTURE %in% 25:30 ~ "Kinki", 
                           PREFECTURE %in% 31:35 ~ "Chugoku", 
                           PREFECTURE %in% 36:39 ~ "Shikoku", 
                           PREFECTURE %in% 40:47 ~ "Kyushu"))

# Abe's supporting speech
votematch.prefecture$ABE <- 0
## 大家敏志 (Fukuoka) / June 24
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 40 & 
                           votematch.prefecture$TIME >= 2] <- 1
## 江島潔 (Yamaguchi) / June 24
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 35 & 
                           votematch.prefecture$TIME >= 2] <- 1
## 末松信介 (Hyogo) / June 26
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 28 & 
                           votematch.prefecture$TIME >= 4] <- 1
## 山崎正昭 (Fukui) / June 27
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 18 & 
                           votematch.prefecture$TIME >= 5] <- 1
## 岡田直樹 (Ishikawa) / June 27
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 17 & 
                           votematch.prefecture$TIME >= 5] <- 1
## 生稲晃子 (Tokyo) / June 27
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 13 & 
                           votematch.prefecture$TIME >= 5] <- 1
## 松川るい (Osaka) / June 28
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 27 & 
                           votematch.prefecture$TIME >= 6] <- 1
## 佐藤啓 (Nara) / June 28
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 29 & 
                           votematch.prefecture$TIME >= 6] <- 1
## 古庄玄知 (Oita) / June 29
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 44 & 
                           votematch.prefecture$TIME >= 7] <- 1
## 山本順三 (Ehime) / June 30
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 38 & 
                           votematch.prefecture$TIME >= 8] <- 1
## 山本佐知子 (Mie) / July 1
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 24 & 
                           votematch.prefecture$TIME >= 9] <- 1
## 長谷川岳 (Hokkaido) / July 2
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 1 & 
                           votematch.prefecture$TIME >= 10] <- 1
## 臼井正一 & 猪口邦子 (Chiba) / July 3
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 12 & 
                           votematch.prefecture$TIME >= 11] <- 1
## 桜井充 (Miyagi) / July 5
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 4 & 
                           votematch.prefecture$TIME >= 13] <- 1
## 星北斗 (Fukushima) / July 6
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 7 & 
                           votematch.prefecture$TIME >= 14] <- 1
## 浅尾慶一郎 (Kanagawa) / July 6
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 14 & 
                           votematch.prefecture$TIME >= 14] <- 1
## 小野田紀美 (Okayama) / July 7
votematch.prefecture$ABE[votematch.prefecture$PREFECTURE == 33 & 
                           votematch.prefecture$TIME >= 15] <- 1

# regression model
## support for LDP
reg1 <- lmer(
  LDP ~ ASSASSINATION + ABE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70 + (1 | BLOCK), 
  data = votematch.prefecture[votematch.prefecture$SIZE >= 50,]
)
## restricting rights to public safety
reg2 <- lmer(
  Q18 ~ ASSASSINATION + ABE + MALE + FEMALE + 
    AGE20 + AGE30 + AGE40 + AGE50 + AGE60 + AGE70 + (1 | BLOCK), 
  data = votematch.prefecture[votematch.prefecture$SIZE >= 50,]
)
## output
msummary(title = "Results of Regression Analysis", 
         models = list("Support for LDP" = reg1, 
                       "Restricting Rights to Public Order" = reg2), 
         stars = TRUE, 
         coef_map = c("(Intercept)" = "(Intercept)", 
                      "ASSASSINATION" = "After July 7, 2022"), 
         fmt = 2, 
         gof_map = c("nobs"), 
         output = "latex")
